
package org.puremvc.as3.multicore.demos.flex.hellopipes.modules.simplemodule.view
{
	import org.puremvc.as3.multicore.demos.flex.hellopipes.common.PipeAwareModuleConstants;
	import org.puremvc.as3.multicore.demos.flex.hellopipes.modules.simplemodule.ApplicationFacade;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
	import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
	
	public class SimpleModuleJunctionMediator extends JunctionMediator
	{
		public static const NAME:String = 'SimpleModuleJunctionMediator';
		
		public function SimpleModuleJunctionMediator( )
		{
			super( NAME, new Junction() );
		}

		/**
		 * Called when the Mediator is registered.
		 */
		override public function onRegister():void
		{
					
		}
		/**
		 * Remove event listeners
		 */
		override public function onRemove():void
		{
			//
			// remove both pipes (shell -> module && module -> shell )			
			junction.removePipe( PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE );
			junction.removePipe( PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE );		
		}		
		/**
		 * ShellJunction related Notification list.
		 * <P>
		 * Adds subclass interests to JunctionMediator interests.</P>
		 */
		override public function listNotificationInterests():Array
		{
 			var interests:Array = super.listNotificationInterests();
			interests.push( ApplicationFacade.SEND_MESSAGE_TO_SHELL );
			return interests; 
		}

		/**
		 * Handle SimpleModule related Notifications.
		 */
		override public function handleNotification( note:INotification ):void
		{
			
			switch( note.getName() )
			{							
				case ApplicationFacade.SEND_MESSAGE_TO_SHELL:
					junction.sendMessage( PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE,
											new Message( PipeAwareModuleConstants.MODULE_TO_SHELL_MESSAGE,
															null,
															note.getBody() ));
					break;				
				// Let super handle the rest (ACCEPT_OUTPUT_PIPE, ACCEPT_INPUT_PIPE, SEND_TO_LOG)								
				default:
					super.handleNotification(note);					
			}	
		}
		
		/**
		 * Handle incoming pipe messages from the Shell.
		 */
		override public function handlePipeMessage( message:IPipeMessage ):void
		{
			switch ( Message(message).getType() )
			{
				case PipeAwareModuleConstants.SHELL_TO_MODULE_MESSAGE:
					sendNotification( ApplicationFacade.MESSAGE_FROM_SHELL_RECEIVED, 
										Message(message).getBody() );
					break;

			}
		}
	}
}